define(function(require){
	var $ = require("jquery");
	var justep = require("$UI/system/lib/justep");

	var Model = function(){
	    this.shopId = '';
	    this.categoryId = '';
		this.callParent();
		this.loadDone = false;
	};

	Model.prototype.categoryDataCustomRefresh = function(event){
	    if(this.shopId){
	    	var me = this;
			var url = this.getParentModel().serverUrl + "/ex_community/a/api/community/getProductCategory?callback=?";
			$.getJSON(url,
			          function(data){
			          		if(data && data.data){
			          			event.source.loadData(data.data);
			          			me.categoryId = data.data[0].id;		          			
			          			me.comp("goodsList").refresh(true);
			          		}
			          });
	    }
	    

	};

	Model.prototype.modelParamsReceive = function(event){	 
		this.shopId = this.getContext().getRequestParameter("shopType");
		this.comp("categoryList").refresh(true);
	};

	Model.prototype.goodsDataCustomRefresh = function(event){
	          
		this.getAndLoadGoodsByCategory(this.categoryId,this.shopId,event);
	};
	
	
	Model.prototype.getImageUrl = function(path){
		return this.getParentModel().serverUrl+path;
	};

	Model.prototype.categoryListClick = function(event){
		if(this.categoryId != this.comp("categoryData").getCurrentRow().val("id")){
			this.categoryId = this.comp("categoryData").getCurrentRow().val("id");
			this.comp("goodsData").clear();
			this.comp("goodsList").refresh(true);
		}
		
	};
	
	Model.prototype.getAndLoadGoodsByCategory = function(categoryId,shopId,event){
	    if(event.source.count() < event.offset){
	    	return;
	    }
	    this.comp("popOver1").show();
	    var me = this;
		var url = this.getParentModel().serverUrl + "/ex_community/a/api/community/getProductByCategoryId?callback=?";
		var page = event.offset/event.limit + 1;
		$.getJSON(
				url,
				{
					"category.id":categoryId,
					"business.id":shopId,
					"pageSize":event.limit,
					"pageNo":page
				},
				function(data){
				    event.source.loadData(data.data,true,null,event.offset,true);
				    me.loadDone = true;
				    me.comp("popOver1").hide();	   			  	
				}			
		);
	
	};

	Model.prototype.goodsListClick = function(event){
	    var currentRow = this.comp("goodsData").getCurrentRow();
		var option = {
				"title":currentRow.val("title"),
				"description":currentRow.val("description"),
				"images":currentRow.val("images"),
				"introduce":currentRow.val("introduce"),
				"price":currentRow.val("price")
		};
		justep.Shell.showPage("$UI/communityOto/home/goods.w",option);
	};

	Model.prototype.backBtnClick = function(event){
		this.close();
	};

	Model.prototype.scrollView6PullDown = function(event){
		    this.comp("goodsData").refreshData(true);
	};

	Model.prototype.scrollView6PullUp = function(event){
	        if(this.loadDone){
	        	this.loadDone = false;
	        	this.comp("goodsData").loadNextPageData();
	        }
			
	};

	Model.prototype.goodsDataBeforeRefresh = function(event){
		this.loadDone = false;
	};

	return Model;
});